package mts.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import mts.ElevatorControllerInternalConnection;
import mts.ElevatorControllerView;
import mts.common.CommonSettings;
import mts.groupdispatcher.ElevatorControllerConnection;
import mts.groupdispatcher.StatusChangedListener;
import mts.messages.FloorRequestMessage;
import mts.messages.StatusMessage;
import mts.utils.FloorUtil;

import org.apache.log4j.Logger;

public class CarPanel extends Panel implements ElevatorControllerView, StatusChangedListener {
	
	
	
	private static final long serialVersionUID = 3529930848093239497L;

	private static final Logger log = Logger.getLogger(CarPanel.class);
	
	private static final Dimension BUTTON_SIZE = new Dimension(25, 25);
	
	
	
	private JTextArea statusTxt;
	private ElevatorControllerInternalConnection elevatorControllerInternalConnection;
	
	public CarPanel(MainGui gui, ElevatorControllerInternalConnection elevatorControllerConnection) {
		
		this.elevatorControllerInternalConnection = elevatorControllerConnection;		
		
		gui.getGroupDispatcher().getElevatorController(elevatorControllerInternalConnection.getCar()).onStatusChangedListener(this);
		
		setLayout(new BorderLayout(5, 5));
		
		// create current floor text box
		statusTxt = new JTextArea();
		statusTxt.setPreferredSize(new Dimension(200, 70));
		
		Font font = statusTxt.getFont();		
		statusTxt.setFont(new Font(font.getFontName(), font.getStyle(), 60));
		
		statusTxt.setForeground(Color.RED);
		statusTxt.setBackground(Color.DARK_GRAY);
		
		add(statusTxt, BorderLayout.NORTH);
		
		// create floor buttons
		JPanel buttonPanel = new JPanel();	
		buttonPanel.setLayout(new GridLayout(10,3));
		for (int i = CommonSettings.NUMBER_OF_FLOORS - 1; i >= 0; i--) {
			JButton floorBtn = new FloorButton((byte)i);
			buttonPanel.add(floorBtn);
		}	
		
		add(buttonPanel);
	}
	
	public void updateStatus(StatusMessage statusMessage) {		
		statusTxt.setText(FloorUtil.getFloorName(statusMessage.getCurrentFloor()) 
				+ " " + 
				statusMessage.getDirection().toString() 
				+ " " + 
				statusMessage.getStatus().toString());
	}
	
	
	private class FloorButton extends JButton {
	
		private static final long serialVersionUID = 26889788523634471L;
 
		public FloorButton(byte floor)  {
			super(FloorUtil.getFloorName(floor));
			
			setPreferredSize(BUTTON_SIZE);
			addActionListener(new ElevatorListener(floor));
			Insets insets = new Insets(1, 1, 1, 1);
			setMargin(insets);
			
			setFont(getFont().deriveFont(2));
			
		}
	}
	
	private class ElevatorListener implements ActionListener {

		private byte floor;
		public ElevatorListener(byte floor) {
			this.floor = floor;
		}
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			log.info("Car Button [floor: "+ FloorUtil.getFloorName(floor) + "] was pressed");
			//log.info("Car Button [car: " + carNum + ", floor: "+ FloorUtil.getFloorName(floor) + "] was pressed");
			elevatorControllerInternalConnection.sendMessage(new FloorRequestMessage(this.floor));
		}
		
	}

	@Override
	public void update(ElevatorControllerConnection elevatorControllerConnection) {
		updateStatus(elevatorControllerConnection.getCurrentStatus());
		
	}
	
	
	
	
	
}
